﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Web.Security;
using Jayrock.Json;

namespace SAMPRO.Web.GFC
{

    public class FriendConnectUtil
    {

        public FriendConnectUtil()
        {
        }

        public static gfcUser getViewer(string fcAuthToken)
        {
            return getGfcUser(fcAuthToken, "@viewer");
        }

        public static gfcUser getGfcUser(string fcAuthToken, string userId)
        {
            HttpProvider httpProvider =  new HttpProvider();
            gfcUser retVal = null;
            string url = "";
            //// try to retrieve the user using fcAuth
            if (!string.IsNullOrEmpty(fcAuthToken))
            {
                url = string.Format("{0}/people/{1}/@self?fcauth={2}",
                    @"https://www.google.com/friendconnect/api"
                    , userId, fcAuthToken);

            }
            else
                return retVal;
            
            string response = httpProvider.send(url, "GET", "", null);
            if (!string.IsNullOrEmpty(response))
            {
                JsonTextReader reader = new JsonTextReader(new StringReader(response));
                JsonObject data = new JsonObject();
                data.Import(reader);

                retVal = new gfcUser();
                JsonObject entry = (JsonObject)data["entry"];
                retVal.gfcProvider = gfcProviders.Google;
                retVal.gfcId = entry["id"].ToString();
                retVal.Image = entry["thumbnailUrl"].ToString();
                retVal.Name = entry["displayName"].ToString();
            }
            return retVal;
        }

        public static gfcUser getOAuthGfcUser(System.Web.HttpApplicationState App, string userId)
        {
            HttpProvider httpProvider = new HttpProvider();
            string url = "";

            // use 2 legged OAuth to fetch the user.
            url = string.Format("{0}/people/{1}/@self",
                gfcAppConfig.GetGfcApiUrl(App)
                , userId);

            url = OAuthUtil.signRequest(
                  url
                , gfcAppConfig.GetGfcOAuthKey(App)
                , gfcAppConfig.GetGfcOAuthSecret(App)
                , ""
                , gfcAppConfig.GetGfcSiteId(App)
                , new Hashtable());

            string response = httpProvider.send(url, "GET", "", null);
            gfcUser retVal = null;

            if (!string.IsNullOrEmpty(response))
            {
                JsonTextReader reader = new JsonTextReader(new StringReader(response));
                JsonObject data = new JsonObject();
                data.Import(reader);

                retVal = new gfcUser();
                JsonObject entry = (JsonObject)data["entry"];
                retVal.gfcProvider = gfcProviders.Google;
                retVal.gfcId = entry["id"].ToString();
                retVal.Image = entry["thumbnailUrl"].ToString();
                retVal.Name = entry["displayName"].ToString();
            }
            return retVal;
        }

        public static Hashtable getViewerGfcFriends(string fcAuthToken, int start, int count)
        {
            HttpProvider httpProvider = new HttpProvider();
            string url = string.Format(
                "{0}/people/@viewer/@friends?fcauth={1}&startIndex={2}&count={3}"
                , @"https://www.google.com/friendconnect/api"
                , fcAuthToken, start, count);
            string response = httpProvider.send(url, "GET", "", null);
            Hashtable retVal = new Hashtable();
            if (!string.IsNullOrEmpty(response))
            {
                JsonTextReader reader = new JsonTextReader(new StringReader(response));
                JsonObject data = new JsonObject();
                data.Import(reader);

                retVal["startIndex"] = int.Parse(data["startIndex"].ToString());
                retVal["count"] = ((JsonArray)data["entry"]).Length;
                retVal["totalResults"] = int.Parse(data["totalResults"].ToString());

                List<gfcUser> users = new List<gfcUser>();
                JsonArray entry = (JsonArray)data["entry"];
                for (int i = 0; i < (int)retVal["count"]; i++)
                {
                    JsonObject jUser = (JsonObject)entry[i];
                    gfcUser user = new gfcUser();
                    user.gfcProvider = gfcProviders.Google;
                    user.gfcId = jUser["id"].ToString();
                    user.Image = jUser["thumbnailUrl"].ToString();
                    user.Name = jUser["displayName"].ToString();
                }
                retVal["friends"] = users.ToArray();
            }
            return retVal;
        }

    }
}